package com.example.design.c.prototype;

import javax.swing.*;
import java.awt.*;

class SunWuKong extends JPanel implements Cloneable {

    private static final long serialVersionUID = 5543049531872119328L;

    public SunWuKong(){
        JLabel l1 = new JLabel(new ImageIcon("src/sunwukong.jpg"));
        this.add(l1);
    }

    @Override
    protected Object clone()  {
        SunWuKong sunWuKong = null;
        try {
            sunWuKong = (SunWuKong) super.clone();

        } catch (CloneNotSupportedException e) {
            System.out.println("拷贝孙悟空失败！");
        }

        return sunWuKong;
    }
}

public class PrototypeWuKong{

    public static void main(String[] args) {
        JFrame jFrame = new JFrame("原型模型测试");
        jFrame.setLayout(new GridLayout(1,2));
        Container container = jFrame.getContentPane();
        SunWuKong obj1 = new SunWuKong();
        container.add(obj1);
        SunWuKong obj2 = (SunWuKong) obj1.clone();
        container.add(obj2);
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}